package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcGiftMapper;
import com.csun.cmny.provider.dto.GiftQueryDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcGift;
import com.csun.cmny.provider.service.OmcGiftService;
import com.csun.cmny.provider.vo.GiftVo;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-05-23 10:15
 * @Description: the class omc gift service impl
 */
@Slf4j
@Service
@Transactional
public class OmcGiftServiceImpl extends BaseService<OmcGift> implements OmcGiftService {

    @Resource
    private OmcGiftMapper omcGiftMapper;

    /**
     * 保存礼品信息
     *
     * @param omcGift      omc gift
     * @param loginAuthDto login auth dto
     * @return the int.
     */
    @Override
    public Integer saveGift(OmcGift omcGift, LoginAuthDto loginAuthDto) {

        omcGift.setUpdateInfo(loginAuthDto);

        if (omcGift.isNew()) {
            // 校验礼品编号是否唯一
            OmcGift omcGiftQuery = new OmcGift();
            omcGiftQuery.setGiftNo(omcGift.getGiftNo());
            int count = omcGiftMapper.selectCount(omcGiftQuery);
            if (count > 0) {
                throw new OmcBizException(ErrorCodeEnum.OMC10031024);
            }

            // 校验礼品名称是否唯一
            omcGiftQuery = new OmcGift();
            omcGiftQuery.setGiftName(omcGift.getGiftName());
            omcGiftQuery.setGroupId(omcGift.getGroupId());
            count = omcGiftMapper.selectCount(omcGiftQuery);
            if (count > 0) {
                throw new OmcBizException(ErrorCodeEnum.OMC10031026);
            }

            return omcGiftMapper.insertSelective(omcGift);
        } else {

            // 校验礼品单号是否唯一
            if (PublicUtil.isNotEmpty(omcGift.getGiftNo())) {
                Example example = new Example(OmcGift.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("giftNo", omcGift.getGiftNo());
                criteria.andNotEqualTo("id", omcGift.getId());

                int count = selectCountByExample(example);
                if (count > 0) {
                    throw new OmcBizException(ErrorCodeEnum.OMC10031024);
                }
            }

            // 校验礼品名称是否唯一
            if (PublicUtil.isNotEmpty(omcGift.getGiftName())) {
                Example example = new Example(OmcGift.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("giftName", omcGift.getGiftName());
                criteria.andEqualTo("groupId", omcGift.getGroupId());
                criteria.andNotEqualTo("id", omcGift.getId());

                int count = selectCountByExample(example);
                if (count > 0) {
                    throw new OmcBizException(ErrorCodeEnum.OMC10031026);
                }
            }

            return omcGiftMapper.updateByPrimaryKeySelective(omcGift);
        }
    }

    /**
     * 分页查询礼品信息
     *
     * @param giftQueryDto gift query dto
     * @return the page info.
     */
    @Override
    public PageInfo queryGiftListWithPage(GiftQueryDto giftQueryDto) {

        PageHelper.startPage(giftQueryDto.getPageNum(), giftQueryDto.getPageSize());
        List<GiftVo> giftVoList = omcGiftMapper.queryGiftListWithPage(giftQueryDto);

        return new PageInfo<>(giftVoList);
    }

    /**
     * 根据Id删除礼品信息
     *
     * @param id 礼品Id
     * @return the int.
     */
    @Override
    public Integer deleteGiftById(Long id) {

        // 校验礼品是否存在
        OmcGift omcGiftQuery = omcGiftMapper.selectByPrimaryKey(id);
        if (PublicUtil.isEmpty(omcGiftQuery)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031018);
        }

        if (omcGiftQuery.getConvertTimes() > 0) {
            throw new OmcBizException(ErrorCodeEnum.OMC10031019);
        }

        return omcGiftMapper.deleteByPrimaryKey(id);
    }

    /**
     * 兑换礼品
     *
     * @param omcGift omc gift
     * @return the int.
     */
    @Override
    public Integer convertGift(OmcGift omcGift) {

        OmcGift omcGiftQuery = new OmcGift();
        omcGiftQuery.setId(omcGift.getId());
        omcGiftQuery.setStock(omcGift.getStock() - 1);
        omcGiftQuery.setConvertTimes(omcGift.getConvertTimes() + 1);

        return omcGiftMapper.updateByPrimaryKeySelective(omcGiftQuery);
    }
}
